package com.cloudbroker.bcs.platform.dav.middle.extservice.hsopen.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.util.IOUtil;
import com.cloudbroker.bcs.platform.dav.common.extservice.store.StoreServiceFactory;
import com.cloudbroker.bcs.platform.dav.middle.entity.Stkcode;
import com.cloudbroker.bcs.platform.dav.middle.entityexample.StockInfo;
import com.cloudbroker.bcs.platform.dav.middle.extservice.hsopen.HsOpenService;
import com.cloudbroker.bcs.platform.dav.middle.extservice.itn.service.ITNService;
import com.cloudbroker.bcs.platform.dav.middle.service.StkcodeService;
import com.cloudbroker.bcs.platform.dav.middle.util.ChineseCharToEn;

/**
 * Created by xuyn15828 on 2016/3/30.
 */
@Service
public class HsOpenServiceImpl implements HsOpenService {
    
    private static String en_hq_type_code = "SS.ESA,SS.ESB,SZ.ESA,SZ.ESB";
    private static String sort_field_name = "business_balance";
    private static String fields = "prod_name";
    private static String start_pos = "0";
    private static String data_count = "10000";
    
    @Resource
    private ITNService itnService;
    @Resource
    private StkcodeService stkcodeService;
    
    @Override
    public void stockUpdate() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("en_hq_type_code", en_hq_type_code);//类型代码集
        param.put("data_count", data_count);//数据个数
        param.put("sort_field_name", sort_field_name);//排序字段名称
        param.put("fields", fields);//字段集合
        param.put("start_pos", start_pos);//起始位置
        
        JSONObject obj = this.itnService.queryStockList(param);
        if (obj != null && obj.size() > 0) {
            Set<String> it = obj.keySet();
            int count = stkcodeService.deleteAll();
            //count = stockCategoryService.deleteAll();
            List<StockInfo> listMap = new ArrayList<StockInfo>();
            count = 1;
            for (String string : it) {
                if (string != "fields") {
                    JSONArray array = obj.getJSONArray(string);
                    Stkcode stkcode = new Stkcode();
                    stkcode.setID(String.valueOf(count++));
                    stkcode.setSecu_code(string.trim());
                    stkcode.setSecu_abbr(string.substring(0, 6));
                    stkcode.setChi_name(String.valueOf(array.get(0)).trim());
                    stkcode.setChi_name_abbr(String.valueOf(array.get(0)).trim());
                    ChineseCharToEn cte = new ChineseCharToEn();
                    String pinyin = cte.getAllFirstLetter(stkcode.getChi_name());
                    stkcode.setChi_spelling(pinyin);
                    stkcodeService.save(stkcode);
                    listMap.add(new StockInfo(stkcode.getChi_name(), stkcode.getSecu_code(), pinyin));
                }
            }
            String json = JSONObject.toJSONString(listMap);
            json = "var STOCK_CODE=" + json;
            try {
                InputStream inputStream = IOUtil.bytesToStream(json.getBytes("UTF-8"));
                String fileName = "STOCKS.js";
                
                //生成到本地
                /*byte[] data = new byte[1024];
                int len = 0;
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream("D:\\STOCKS.js");
                    while ((len = inputStream.read(data)) != -1) {
                        fileOutputStream.write(data, 0, len);
                        
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    
                }*/
                
                // 上传到服务器
                StoreServiceFactory.getInstance().getStoreService().uploadFile(fileName, inputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
